package com.qingfeng.testandroid12sdkdemo.activity

import android.animation.ObjectAnimator
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.AnticipateInterpolator
import androidx.core.animation.doOnEnd
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.qingfeng.basic_lib.basic.BasicActivity
import com.qingfeng.basic_lib.basic.NoViewModel
import com.qingfeng.testandroid12sdkdemo.R
import com.qingfeng.testandroid12sdkdemo.databinding.ActivityMainBinding
import com.qingfeng.testandroid12sdkdemo.fragment.HomeFragment
import com.qingfeng.testandroid12sdkdemo.fragment.MeFragment
import com.qingfeng.testandroid12sdkdemo.fragment.ProjectFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener
import javax.inject.Inject



//@AndroidEntryPoint
class MainActivity :BasicActivity<NoViewModel , ActivityMainBinding>() {

    private val fragments = ArrayList<Fragment>()

//    @Inject
//    lateinit var homeFragment: HomeFragment


//    lateinit var projectFragment: ProjectFragment


//    lateinit var meFragment: MeFragment



    override fun initView(savedInstanceState: Bundle?) {
//        mBinding.contentRl.viewTreeObserver.addOnPreDrawListener {
//            object : ViewTreeObserver.OnPreDrawListener {
//                override fun onPreDraw(): Boolean {
//                    // Check if the initial data is ready.
//                    return if (viewModel.isReady) {
//                        // The content is ready; start drawing.
//                        mBinding.contentRl.viewTreeObserver.removeOnPreDrawListener(this)
//                        true
//                    } else {
//                        // The content is not ready; suspend.
//                        false
//                    }
//                }
//            }
//        }
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.design_default_color_secondary))
        fragments.add(HomeFragment.newInstance())
        fragments.add(ProjectFragment.newInstance())
        fragments.add(MeFragment.newInstance())
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container, fragments[0])
            .commitNow()

        val navCtl = mBinding.pageNavigationView.material()
            .addItem(R.drawable.tab_shop_selected, "首页")
            .addItem(R.drawable.tab_car_selected, "项目")
            .addItem(R.drawable.tab_me_selected, "我的")
            .dontTintIcon()
            .build()

        navCtl.addTabItemSelectedListener(object : OnTabItemSelectedListener{
            override fun onSelected(index: Int, old: Int) {
                switchPage(index, old)
            }

            override fun onRepeat(index: Int) {

            }
        })



    }

    private fun switchPage(index: Int, old: Int) {
        val now = fragments[index]
        supportFragmentManager.beginTransaction().apply {
            if (!now.isAdded) {
                add(R.id.container, now)
            }
            show(now)
            hide(fragments[old])
            commit()
        }
    }

    override fun initSplashMethod() {
        if (Build.VERSION.SDK_INT >= 31) {
            splashScreen.setOnExitAnimationListener { splashScreenView ->
                val slideUp = ObjectAnimator.ofFloat(
                    splashScreenView,
                    View.TRANSLATION_Y,
                    0f,
                    -splashScreenView.height.toFloat()
                )
                slideUp.interpolator = AnticipateInterpolator()
                slideUp.duration = 200L

                slideUp.doOnEnd {
                    splashScreenView.remove()
                }
                slideUp.start()
            }
        }

    }

    override fun initData() {


//        mBinding.pageNavigationView.
    }


    private fun registerFlow(){
        lifecycleScope.launch {
//            val flow = testFlow().stateIn(scope = lifecycleScope)
            lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {
                LogUtils.e("is run repeatOnLifecycle Lifecycle.State.CREATED ")
                testFlow().stateIn(scope = lifecycleScope).collect {
                    LogUtils.e("value = $it")
//                    mBinding.showTv.text = "value = $it"
                }
            }
        }
    }

    private fun testFlow(): Flow<Int> {
        val flow = flow {
            for (i in 0 until 5) {
                emit(i)
                delay(1000)
            }
        }
        return flow
    }



}
